﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;


namespace CRM.Web.MenuManage
{
    public partial class RoleMenuManage : Common.PageBase
    {
        T_ROLE_BLL rBll = new T_ROLE_BLL();
        T_MENU_BLL mBll = new T_MENU_BLL();
        T_ROLEMENU_BLL rmBll = new T_ROLEMENU_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlROLEIDBinding();
                tvMenuBinding();
            }
        }

        #region 页面初始化数据绑定



        private void ddlROLEIDBinding()
        {
            StringBuilder sql = new StringBuilder();
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            ddlROLEID.DataSource = rBll.GetList(sql.ToString());
            ddlROLEID.DataTextField = "RoleName";
            ddlROLEID.DataValueField = "RoleID";
            ddlROLEID.DataBind();
        }







        #endregion


        private void tvMenuBinding()
        {
            tvMenu.Nodes.Clear();
            List<T_MENU_Entity> list = mBll.GetList(" and MenuState = '0' ");

            TreeNode node = new TreeNode();
            node.Text = "菜单";
            MenuRecursiveBinding(list, "0", node);
            tvMenu.Nodes.Add(node);
            tvMenu.ExpandAll();

        }

        /// <summary>
        /// 递归 绑定下接列表。
        /// </summary>
        /// <param name="list"></param>
        /// <param name="parentID"></param>
        /// <param name="ddl"></param>
        private void MenuRecursiveBinding(List<T_MENU_Entity> list, string parentID, TreeNode parentNode)
        {

            List<T_MENU_Entity> sublist = list.FindAll(delegate(T_MENU_Entity m) { if (m.PARENTMENUID.ToString() == parentID) return true; return false; });

            string menuID = "";
            string roleID = ddlROLEID.SelectedValue.Trim();

            foreach (T_MENU_Entity entity in sublist)
            {
                TreeNode node = new TreeNode(entity.MENUNAME.Trim(), entity.MENUID.ToString());

                menuID = entity.MENUID.ToString();
                node.Checked = rmBll.GetCache_T_ROLEMENU_List(this.Page).Exists(delegate(T_ROLEMENU_Entity en) { if (en.ROLEID.ToString().Equals(roleID) && en.MENUID.ToString().Equals(menuID)) return true; else return false; });


                MenuRecursiveBinding(list, entity.MENUID.ToString(), node);
                parentNode.ChildNodes.Add(node);
            }

        }


        protected void ddlROLEID_SelectedIndexChanged(object sender, EventArgs e)
        {
            tvMenuBinding();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!StringHelper.IsNumber(ddlROLEID.SelectedValue.Trim()))
            {
                PageHelper.ShowMessage("请选择角色！");
                return;
            }
            try
            {
                rmBll.Delete("RoleID", ddlROLEID.SelectedValue.Trim());
                foreach (TreeNode node in tvMenu.Nodes)
                {
                    TraversalChildTree(node, StringHelper.ToInt(ddlROLEID.SelectedValue.Trim()));
                }
                PageHelper.ShowMessage("角色菜单权限设置成功!");
            }
            catch (Exception ex)
            {
                PageHelper.ShowMessage("角色菜单权限设置失败!");
            }

        }

        protected void TraversalChildTree(TreeNode parentNode, int roleID)
        {
            if (parentNode.Checked)
            {
                int menuID = 0;
                if (int.TryParse(parentNode.Value, out menuID))
                {
                    T_ROLEMENU_Entity entity = new T_ROLEMENU_Entity();
                    entity.MENUID = menuID;
                    entity.ROLEID = roleID;
                    rmBll.Insert(entity);
                }
            }
            foreach (TreeNode node in parentNode.ChildNodes)
            {
                TraversalChildTree(node, roleID);
            }
        }

        
    }
}
